const app = getApp();
import * as Api from "../../api/login";
import { getApiUrl } from "../../utils/common";
const defaultAvatarUrl =
  "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isHome: true,
    hasUserInfo: false,
    userInfo: {
      headimg: defaultAvatarUrl,
      nickname: "微信用户",
      mobile: wx.getStorageSync("mobile"),
    },
    agreementUrl: `/pages/webview/webview?link=${app.globalData.agreement}`,
    list: [
      {
        name: "我的订单",
        icon: "../../images/my-icon-1.png",
        url: "/pages/orderList/orderList",
      },
      {
        name: "发票申请",
        icon: "../../images/my-icon-2.png",
        url: "/pages/notInvoicedOrders/notInvoicedOrders",
      },
      {
        name: "开票历史",
        icon: "../../images/my-icon-3.png",
        url: "/pages/invoiceList/invoiceList",
      },
      {
        name: "退出登录",
        icon: "../../images/my-icon-4.png",
        url: "/pages/index/index",
        type: "logout",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    Api.getUserInfo(app).then((res) => {
      wx.setStorageSync("mobile", res.mobile);
      this.setData({
        userInfo: {
          nickname: res.nickname,
          headimg: res.headimg,
          mobile: res.mobile,
        },
        hasUserInfo: res.nickname && res.headimg,
      });
    });
  },
  goToPages(e) {
    console.log(e);
    const { type, url } = e.currentTarget.dataset;
    if (type == "logout") {
      Api.logout(app).then(() => {
        // 退出登录
        wx.showToast({
          title: "退出成功",
          icon: "none",
          success: () => {
            wx.removeStorageSync("mobile");
            wx.removeStorageSync("token");
            wx.removeStorageSync("is_login");
            wx.navigateTo({ url });
          },
        });
      });
    } else {
      wx.navigateTo({ url });
    }
  },
  /**
   * @description: 获取用户头像
   */
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    console.log("avatarUrl", avatarUrl);
    console.log("getApiUrl", getApiUrl, getApiUrl());

    wx.uploadFile({
      url: getApiUrl() + "/common/upload_headimg/",
      filePath: avatarUrl,
      name: "file",
      header: { ...app.header(), "Content-Type": "multipart/form-data" },
      success: (res) => {
        console.log("res", res);
        const data = JSON.parse(res.data);
        console.log("data", data);

        Api.saveUserInfo(app, {
          headimg: data.data.headimg_url,
        }).then(() => {
          this.setData({
            "userInfo.headimg": data.data.headimg_url,
          });
        });
      },
      fail: (err) => {
        console.log("err", err);
      },
    });
    // app
    //   .uploadFile("/api/v1/common/upload_headimg/", {
    //     file: avatarUrl,
    //   })
    //   .then((res) => {
    //     Api.saveUserInfo(app, {
    //       headimg: res.headimg_url,
    //     }).then(() => {
    //       this.setData({
    //         "userInfo.headimg": res.headimg_url,
    //       });
    //     });
    //   });
  },
  /**
   * @description: 获取用户昵称
   */
  handleNickname(e) {
    Api.saveUserInfo(app, {
      nickname: e.detail.value,
    }).then(() => {
      this.setData({
        "userInfo.nickname": e.detail.value,
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
});
